package lc.q201_250;

import lc.ListNode;

/**
 * 移除链表元素，移除链表中所有等于val的数
 *
 * @author lgb
 * @date 2020/08/23
 **/
public class Q203 {

    public ListNode removeElements(ListNode head, int val) {
        // 先获取第一个不是val的头
        while (head != null && head.val == val) {
            head = head.next;
        }
        // 然后逐步的去弄
        ListNode next = head;
        while (next != null) {
            while (next.next != null && next.next.val == val) {
                ListNode next1 = next.next.next;
                next.next.next = null; // for gc
                next.next = next1;
            }
            next = next.next;
        }
        return head;
    }

}
